package se.sll.service.catalogue.common.exception;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * @author Christer Palm, Nogui
 */
public abstract class ServiceErrorCodeException extends ServiceException {
    private final ServiceErrorCode errorCode;
    private final Object[] args;

    protected ServiceErrorCodeException(ServiceErrorCode errorCode, Object[] args) {
        this.errorCode = errorCode;
        this.args = args;
    }

    protected ServiceErrorCodeException(ServiceErrorCode errorCode) {
        this(errorCode, null);
    }

    public ServiceErrorCode getErrorCode() {
        return errorCode;
    }

    public Object[] getArgs() {
        return args;
    }

    public String getMessage(Locale locale) {
        ResourceBundle bundle = ResourceBundle.getBundle("se.sll.service.catalogue.messages.ServiceError", locale);
        String messageTemplate = bundle.getString(errorCode.name());
        String message = MessageFormat.format(messageTemplate, args);
        return message;
    }

    @Override
    public String getMessage() {
        return getMessage(Locale.getDefault());
    }
}
